package com.example.zhuyu.basemvp.view;

import android.app.DialogFragment;
import android.app.FragmentManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.zhuyu.basemvp.R;


/**
 * 网络加载框Fragment
 */
public class LoadingDialogFragment extends DialogFragment{

    private Window mWindow;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        //获取window对象
        mWindow = getDialog().getWindow();
        //设置无标题
        mWindow.requestFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.loading_view,container,false);
        TextView tvMsg = view.findViewById(R.id.tv_msg);
        Bundle bundle = getArguments();
        if(bundle != null){
            String msg = bundle.getString("msg");
            if(TextUtils.isEmpty(msg)){
                tvMsg.setText(msg);
            }
        }
        //透明状态栏
        mWindow.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //清理背景变暗
        mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        //点击外部不消失
        setCancelable(false);
        //设置背景颜色,只有设置了这个属性,宽度才能全屏MATCH_PARENT
        mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams mWindowAttributes = mWindow.getAttributes();
        //gravity
        mWindowAttributes.gravity = Gravity.CENTER;
        mWindow.setAttributes(mWindowAttributes);
        return view;

    }

    /**
     * 根据屏幕密度设置加载框的宽高
     */
    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        mWindow.setLayout((int) (dm.widthPixels * 0.35), (int) (dm.widthPixels * 0.35));
    }

    /**
     * 显示加载框
     * @param manager
     */
    public void show(FragmentManager manager){
        super.show(manager,"loading");

    }

    /**
     * 隐藏加载框
     */
    public void hide(){
        dismiss();
    }
}
